#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import pygame, sys

# 初始化
pygame.init()
SCREEN = pygame.display.set_mode((400, 300))
pygame.display.set_caption('红蓝大碰撞')

# 绿色方块固定在最下方，左右移动，y值不变
blue_rect = pygame.Rect(110, 250, 100, 50)
red_rect = pygame.Rect(85, 0, 20, 50)

class Block(pygame.sprit.Sprit):
    def __init__(shape, color, position):
        self.shape, self.color = shape, color
        self.position = position

    def draw(self):
        pygame.draw.rect(SCREEN, self.color, self.toRect)

    def toRect(self):
        return pygame.Rect(self.position+self.shape)

    def __mul__(self, other):
        return self.toRect().colliderect(other.toRect())


goal = False
while True: 
    for event in pygame.event.get():
        # 处理退出事件
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        # 键盘按下事件
        elif event.type == pygame.KEYDOWN:
            # 'a'键被按下
            if event.key == pygame.K_a:
                blue_rect.x -= 5
            elif event.key == pygame.K_d:
                blue_rect.x += 5
    red_rect.y += 5
    if red_rect.y > 300:
        red_rect.y = 0
    red_rect = pygame.Rect(85, red_rect.y, 20, 50)
    if blue_rect.colliderect(red_rect) or goal is True:
        goal = True
    else:
        SCREEN.fill((255, 255, 255))
        # 调用 pygame.display.update() 方法更新整个屏幕的显示
        pygame.draw.rect(SCREEN, (255, 0, 0), red_rect)
        pygame.draw.rect(SCREEN, (0,  0, 255), blue_rect)
        pygame.display.update()
        pygame.time.delay(50)